package com.edward.service;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.RunCost;


public class RunCostManager extends HibernateDaoSupport {
	
	public void addRunCost(RunCost runcost) {
		
		getHibernateTemplate().save(runcost);
	}
	
	public void updateRunCost(RunCost runcost) {
		
		getHibernateTemplate().update(runcost);
	}
	
	public void deleteRunCost(int id){
		
		getHibernateTemplate().delete(getRunCost(id));
	}
	
	public RunCost getRunCost(int id){
		RunCost d =(RunCost)getHibernateTemplate().get(RunCost.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<RunCost> listRunCost() {
		return getHibernateTemplate().find("from RunCost");
	}

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit) {
		List <RunCost>list = getHibernateTemplate().find("from RunCost" );
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <RunCost>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	
	@SuppressWarnings("unchecked")
	public RunCost getSum(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String d = sdf.format(date);
		List<RunCost> list = getHibernateTemplate().find("from RunCost r where r.date = '" + d + "' and r.type = 't'"  );
		return list.get(0);		
		
	}
	
	@SuppressWarnings("unchecked")
	//判断有没有该月份的合计，如果没有就添加 设type 为T
	public void addcheck (RunCost r){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String d = sdf.format(r.getDate());
		List<RunCost> list = getHibernateTemplate().find("from RunCost r where r.date = '" + d + "' and r.type = 't'"  );
		if(list.size()==0){
			r.setType("t");
			r.setId(0);
			addRunCost(r);
		}
		else{
			RunCost rc = list.get(0);
			rc.setArk(rc.getArk() + r.getArk());
			rc.setBonus(rc.getBonus() + r.getBonus());
			rc.setBuySum(rc.getBuySum() + r.getBuySum());
			rc.setCard(rc.getCard() + r.getCard());
			rc.setCustoms(rc.getCustoms() + r.getCustoms());
			rc.setElec(rc.getElec() + r.getElec());
			rc.setFreight(rc.getFreight() + r.getFreight());
			rc.setInsure(rc.getInsure() + r.getInsure());
			rc.setOthers(rc.getOthers() + r.getOthers());
			rc.setPresscar(rc.getPresscar() + r.getPresscar());
			rc.setSeal(rc.getSeal() + r.getSeal());
			rc.setStack(rc.getStack() + r.getStack());
			rc.setTrailer(rc.getTrailer() + r.getTrailer());
			rc.setSum(rc.getSum() + r.getSum());
			updateRunCost(rc);
			
		}
	}
	

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,String query) {
		if(query.equals("")){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			query = sdf.format(new Date());
		}
		
		List <RunCost>list = getHibernateTemplate().find("from RunCost f where f.type = 't' and year(f.date) = '" + query + "'");
		RunCost rc = new RunCost(); 
		for (RunCost r : list) {
			rc.setArk(rc.getArk() + r.getArk());
			rc.setBonus(rc.getBonus() + r.getBonus());
			rc.setBuySum(rc.getBuySum() + r.getBuySum());
			rc.setCard(rc.getCard() + r.getCard());
			rc.setCustoms(rc.getCustoms() + r.getCustoms());
			rc.setElec(rc.getElec() + r.getElec());
			rc.setFreight(rc.getFreight() + r.getFreight());
			rc.setInsure(rc.getInsure() + r.getInsure());
			rc.setOthers(rc.getOthers() + r.getOthers());
			rc.setPresscar(rc.getPresscar() + r.getPresscar());
			rc.setSeal(rc.getSeal() + r.getSeal());
			rc.setStack(rc.getStack() + r.getStack());
			rc.setTrailer(rc.getTrailer() + r.getTrailer());
			rc.setSum(rc.getSum() + r.getSum());
			
		 }
		rc.setDate(null);
		list.add(rc);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <RunCost>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerItems(int start,int limit,Date date) {
		
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String query = sdf.format(date);
		List <RunCost>list = getHibernateTemplate().find("from RunCost r where r.type is null and r.date = '" + query + "'");
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <RunCost>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	


}



